namespace Asmex.FileViewer
{
    using Asmex.ObjViewer;
    using System;
    using System.Collections;
    using System.IO;

    public class ImportDirectoryEntry : Region
    {
        private uint _DateTimeStamp;
        private uint _ForwarderChain;
        private uint[] _ImportAddressTable = new uint[0];
        private ImportAddress[] _ImportLookupTable = new ImportAddress[0];
        private string _Name;

        public ImportDirectoryEntry(BinaryReader reader, MModule mod)
        {
            base.Start = reader.BaseStream.Position;
            uint rva = reader.ReadUInt32();
            this._DateTimeStamp = reader.ReadUInt32();
            this._ForwarderChain = reader.ReadUInt32();
            uint num2 = reader.ReadUInt32();
            this._Name = mod.StringFromRVA(reader, num2);
            uint num3 = reader.ReadUInt32();
            base.Length = reader.BaseStream.Position - base.Start;
            long position = reader.BaseStream.Position;
            if (num2 == 0)
            {
                this._Name = null;
            }
            else
            {
                try
                {
                    ArrayList list;
                    uint num5;
                    uint num6;
                    if (rva != 0)
                    {
                        list = new ArrayList();
                        num5 = mod.ModHeaders.Rva2Offset(rva);
                        reader.BaseStream.Position = num5;
                        for (num6 = reader.ReadUInt32(); num6 != 0; num6 = reader.ReadUInt32())
                        {
                            list.Add(new ImportAddress(num6, reader, mod));
                        }
                        this._ImportLookupTable = (ImportAddress[]) list.ToArray(typeof(ImportAddress));
                    }
                    if (num3 != 0)
                    {
                        list = new ArrayList();
                        num5 = mod.ModHeaders.Rva2Offset(num3);
                        reader.BaseStream.Position = num5;
                        for (num6 = reader.ReadUInt32(); num6 != 0; num6 = reader.ReadUInt32())
                        {
                            list.Add(num6);
                        }
                        this._ImportAddressTable = (uint[]) list.ToArray(typeof(uint));
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    reader.BaseStream.Position = position;
                }
            }
        }

        [ObjViewer(Hex=true)]
        public uint DateTimeStamp
        {
            get
            {
                return this._DateTimeStamp;
            }
        }

        [ObjViewer(Hex=true)]
        public uint ForwarderChain
        {
            get
            {
                return this._ForwarderChain;
            }
        }

        [ObjViewer(false)]
        public uint[] ImportAddressTable
        {
            get
            {
                return this._ImportAddressTable;
            }
        }

        [ObjViewer(false)]
        public ImportAddress[] ImportLookupTable
        {
            get
            {
                return this._ImportLookupTable;
            }
        }

        public string Name
        {
            get
            {
                return this._Name;
            }
        }
    }
}
